//
//  ReadListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *newestTableView; //最新列表
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;    //最热列表
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;     //滑动视图
@property (nonatomic,strong)UISegmentedControl *segementControl;   //分段控制器

@property (nonatomic,assign)NSInteger newStart;  //最新列表的数据的起始位置
@property (nonatomic,assign)NSInteger newLimit;  //最新列表的数据的条数
@property (nonatomic,assign)NSInteger hotStart;  //最热列表的数据的起始位置
@property (nonatomic,assign)NSInteger hotLimit;  //最热列表的数据的条数
@property (nonatomic,strong)NSMutableArray *newestDataSource;  //最新列表数据源
@property (nonatomic,strong)NSMutableArray *hotDataSourcr;     //最热列表数据源
@end

@implementation ReadListViewController
- (UISegmentedControl *)segementControl{
    if (!_segementControl) {
        _segementControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        //插入标签
        [_segementControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segementControl insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        [_segementControl addTarget:self action:@selector(clickSegementAction:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _segementControl;
}
- (NSMutableArray *)newestDataSource{
    if (!_newestDataSource) {
        _newestDataSource = [[NSMutableArray alloc]init];
    }
    return _newestDataSource;
}
- (NSMutableArray *)hotDataSourcr{
    if (!_hotDataSourcr) {
        _hotDataSourcr = [[NSMutableArray alloc]init];
    }
    return _hotDataSourcr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;  //防止跑偏
    self.navigationItem.titleView = self.segementControl;
    
    [self.newestTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];

    
    
    //数据请求
    [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    [self loadHotTableViewDataSourceWithStart:0 limit:10];
    
    
    //添加下拉刷新
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        [self.newestDataSource removeAllObjects];
        self.newStart = 0;
        [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    }];

    //添加上拉加载
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart += 10;
        [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    }];
    
}
//segement事件
- (void)clickSegementAction:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [self.scrollview setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma mark---------数据请求------------
//加载最新列表的数据
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kReadListUrl
    parameter:@{@"typeid":@(self.typeID),
                @"start":@(start),
                @"limit":@(limit),
                @"sort":@"addtime"} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        for (NSDictionary *smllaDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smllaDic];
            
        [self.newestDataSource addObject:model];
        }
        
    } updata:^{
        NSLog(@"刷新UI");
        //结束上拉加载
        [self.newestTableView.mj_footer endRefreshing];
        //结束下拉刷新
        [self.newestTableView.mj_header endRefreshing];
        //刷新tableView
        [self.newestTableView reloadData];
    }];
}
//加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    
    [RequestManager fetchDataWithUrl:kReadListUrl
    parameter:@{@"typeid":@(self.typeID),
                @"start":@(start),
                @"limit":@(limit),
                @"sort":@"hot"} mehtod:@"POST" compelet:^(NSData *data) {
                    
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        for (NSDictionary *smllaDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smllaDic];
            [self.hotDataSourcr addObject:model];
        }

    } updata:^{
        
        [self.hotTableView reloadData];
    }];

}


#pragma mark---------ScrollViewDelegate---------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //判断滚动的是不是要滚动的
    if (scrollView == self.scrollview) {
        if (self.scrollview.contentOffset.x == 0) {
            self.segementControl.selectedSegmentIndex = 0;
        }else if (self.scrollview.contentOffset.x == kWidth){
            self.segementControl.selectedSegmentIndex = 1;
        }
    }
}


#pragma mark---------TableViewDelegate----------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.newestTableView) {
        return self.newestDataSource.count;
    }else{
        return self.hotDataSourcr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建一个model来接收
    ReadListModel *model = [[ReadListModel alloc]init];
    if (tableView == self.newestTableView) {
        model = self.newestDataSource[indexPath.row];
    }else{
        model = self.hotDataSourcr[indexPath.row];

    }
    //工厂模式生产出来的cell
    BaseCell *cell = [FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];

    return cell;
}
//cell点击事件
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    ReadListInfoViewController *info = [[ReadListInfoViewController alloc]init];
    if (tableView == self.newestTableView) {
        info.readListModel = self.newestDataSource[indexPath.row];
    }else{
        info.readListModel = self.hotDataSourcr[indexPath.row];

    }
    
    [self.navigationController pushViewController:info animated:YES];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
